package org.androbuzz;

public class AndroBuzzSummaryCleaner {
	
	public AndroBuzzSummaryCleaner() {
		
	}
	
	public String cleanSummary(String parString)
	{
		return cleanSummary(parString, -1);
	}
	
	public String cleanSummary(String parString, int parNbChar)
	{
		String result = "";
		if(parString != null)
		{
			boolean inBalise = false;
			boolean skipBalise = false;
			String holdBaliseName = "";
			String baliseName = "";
			for(int i = 0; i < parString.length(); i++)
			{
				if(result.length() >= parNbChar && parNbChar != -1)
				{
					result += "[...]";
					break;
				}
					
				
				char currentChar = parString.charAt(i);
				if(!inBalise)
				{
					if(currentChar == '<')
					{
						inBalise = true;
					}
					else if(!skipBalise)
					{
						if(currentChar != '\n')
							result += currentChar;
						else
							result += " ";
					}
						
				}
				else
				{
					if(currentChar == '>')
					{
						if(!skipBalise)
						{
							if(baliseNeedSkipContent(baliseName) == true)
							{
								skipBalise = true;
								holdBaliseName = baliseName;
							}
						}
						else
						{
							if(baliseName == holdBaliseName)
							{
								skipBalise = false;
								holdBaliseName = "";
							}
						}
						inBalise = false;
						baliseName = "";
					}
					else if(currentChar != '/')
					{
						baliseName += currentChar;
					}
				}
			}
		}
		return result;
	}
	
	private boolean baliseNeedSkipContent(String parBaliseName)
	{
		return(parBaliseName == "abbr" || 
				parBaliseName == "acronym" || 
				parBaliseName == "applet" || 
				parBaliseName == "bdo" || 
				parBaliseName == "button" || 
				parBaliseName == "caption" || 
				parBaliseName == "commment" || 	
				parBaliseName == "del" || 
				parBaliseName == "fieldset" || 
				parBaliseName == "form" || 
				parBaliseName == "head" || 
				parBaliseName == "ins" || 
				parBaliseName == "label" || 
				parBaliseName == "layer" || 
				parBaliseName == "legend" || 
				parBaliseName == "map" || 
				parBaliseName == "noembed" || 
				parBaliseName == "object" || 
				parBaliseName == "param" || 
				parBaliseName == "script" || 
				parBaliseName == "select" || 
				parBaliseName == "table" || 
				parBaliseName == "textarea" || 
				parBaliseName == "thead" || 
				parBaliseName == "title");
	}
	
}
